package com.yc.resgoods.controller;


import com.yc.bean.Resorder;
import com.yc.bean.Resuser;
import com.yc.resgoods.biz.ResorderBiz;
import com.yc.resgoods.model.CartItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

@RequestMapping("order")
@RestController
@Slf4j
public class OrderController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ResorderBiz resorderBiz;
    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("orderFood")
    public Map<String,Object> orderFood(Resorder resorder, @RequestHeader("Authorization") String bearerToken){
        Map<String,Object> map = new HashMap<>();
        if(!this.redisTemplate.hasKey("cart_"+bearerToken) || this.redisTemplate.opsForHash().entries("cart_" + bearerToken).size() <=0){
            map.put("code",0);
            return map;
        }
        //有购物车数据
        Map<String, CartItem> cart = this.redisTemplate.opsForHash().entries("cart_"+bearerToken);
        Collection<CartItem> cis = (Collection<CartItem>)cart.values();

        Resuser user = new Resuser();
//        String url = "http://localhost:8001/resfood/hello";
        String url = "http://cloud-security/ressecurity/resfood/hello";
        HttpHeaders header = new HttpHeaders(){{
            set("Authorization",bearerToken);
        }};
        ResponseEntity<Map> re = restTemplate.exchange
                (url, HttpMethod.GET,new HttpEntity<Map>(header) ,Map.class);
        Map m = re.getBody();
        int userid = Integer.parseInt(m.get("userid").toString());
        user.setUserid(userid);
        //处理下单时间
        //创建LocalDateTime对象 表示当前日期和时间
        LocalDateTime now = LocalDateTime.now();
        //创建DateTimeFormatter对象，指定格式为“yyyy-MM-dd HH:mm"
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        //调用format方法 将LocalDateTime对象转化为字符串
        resorder.setOrdertime(formatter.format(now));
        LocalDateTime deliveryTime = now.plusHours(5);
        resorder.setDeliverytime(formatter.format(deliveryTime));
        resorder.setStatus(0);
        resorderBiz.order(resorder,new HashSet(cis),user);
        double total = 0;
        for(CartItem ci:cis){
            total += ci.getSmallCount();
        }
        this.redisTemplate.delete("cart_"+bearerToken);
        map.put("code",1);
        return map;
    }
}
